package cn.tom.controller.fore;

import cn.tom.tools.TokenAndUser;
import cn.tom.tools.XJson;
import cn.tom.tools.XResp;
import cn.tom.entity.*;
import cn.tom.service.OrderService;
import cn.tom.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/fore/ord")
public class foreOrdController {
    @Resource
    private OrderService orderService;


    @GetMapping("/addPay/{ordid}")
    public XJson doPay(@PathVariable Integer ordid){
        int n = 0;
        try{
            n =  orderService.doPay(ordid);
            System.out.println(n);
        }catch (MyException e){ return XResp.pack(501,e.getMessage(),null);
        }
        if( n == 1 ) {
            return XResp.pack(200,"支付成功,等待商家发货",null);
        }
        return XResp.pack(501,"支付失败",null);
    }

    @GetMapping("/findByOrdid/{ordid}")
    public XJson findByOrdid(@PathVariable("ordid")Integer ordid)
    {
        Order order = orderService.findByOrdid(ordid);
        System.out.println(order);
        return XResp.pack(200,"",order);
    }

    //查看我的订单分页------付款状态---发货状态--多少天以内-------------------------------------------------------------------------
    @PostMapping("/findPageOrders/{pagenum}/{lines}")
    public XJson findOrdersByPages(Integer payStaus
                                 ,Integer shipStatus
                                 ,Integer days
                                 ,@PathVariable("pagenum") Integer pagenum
                                 ,@PathVariable("lines")   Integer lines)
    {
        log.info("\t" + payStaus + "\t" + shipStatus + "\t" + days + "\t" + pagenum
            + "\t" + lines);
        XUser data = (XUser) TokenAndUser.findOne().getData();
        System.out.println(data);
        HashMap<String,Object> map = new HashMap<>();
        map.put("uid",data.getId());
        map.put("payStatus",payStaus);
        map.put("shipStatus",shipStatus);
        map.put("days",days);
        if(pagenum < 1) pagenum = 1;
        if(lines < 2) lines = 2;
        List<Order> ordersByPages = orderService.findOrdersByPages( map ,pagenum ,lines);
        int total = orderService.total(map);
        HashMap<String,Object> mm = new HashMap<>();
        mm.put("orders" , ordersByPages );
        mm.put("total",total);
        return XResp.pack(200,"",mm);

    }

    @GetMapping("/updateSign/{ordid}")
    public XJson updateSign(@PathVariable Integer ordid)
    {
        int x = orderService.updateShip(ordid,2,null,null,new Date());
        if(x == 1)
            return XResp.pack(200,"更新发货状态...1，签收成功",null);
        return XResp.pack(501,"签收失败！！!",null);
    }



}
